package com.dtjd.playcoinmonkey.activities;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.dtjd.playcoinmonkey.MyApplication;
import com.dtjd.playcoinmonkey.R;
import com.dtjd.playcoinmonkey.adapter.TipOffExpandAdapter;
import com.dtjd.playcoinmonkey.bean.HomeListBean;
import com.dtjd.playcoinmonkey.bean.HttpParaBean;
import com.dtjd.playcoinmonkey.bean.ManageTipOffBean;
import com.dtjd.playcoinmonkey.bean.ManageTipOffPostBean;
import com.dtjd.playcoinmonkey.bean.ManageTipOffReasonBean;
import com.dtjd.playcoinmonkey.databinding.ActivityManageTipOffBinding;
import com.dtjd.playcoinmonkey.utils.Network;
import com.dtjd.playcoinmonkey.utils.Tools;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class ManageTipOffActivity extends AppCompatActivity {
    private int current=1;
    private final List<ManageTipOffBean> data=new ArrayList<>();
    private TipOffExpandAdapter adp;

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (android.R.id.home==item.getItemId()){ finish(); }
        return super.onOptionsItemSelected(item);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityManageTipOffBinding binding = ActivityManageTipOffBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        ActionBar actionbar = getSupportActionBar();
        if (actionbar!=null){
            actionbar.setDisplayHomeAsUpEnabled(true);
            actionbar.setHomeButtonEnabled(true);
        }
        setTitle(getResources().getString(R.string.manageTipoff));
        binding.manageTipoffRefresh.setRefreshHeader(new ClassicsHeader(this));
        binding.manageTipoffRefresh.setRefreshFooter(new ClassicsFooter(this));
        binding.manageTipoffRefresh.setOnRefreshListener(refreshLayout -> refresh(true,null,refreshLayout));
        binding.manageTipoffRefresh.setOnLoadMoreListener(refreshLayout -> refresh(false,refreshLayout,null));
        adp=new TipOffExpandAdapter(this, data, (v, position) -> {
            List<HttpParaBean> para = new ArrayList<>();
            para.add(new HttpParaBean("token", "token"));
            para.add(new HttpParaBean("id", String.valueOf(data.get(position).manageTipOffPostBean.id)));
            Network.post(para, "admin/bannedPost", new Callback() {
                @Override
                public void onFailure(@NonNull Call call, @NonNull IOException e) {
                    e.printStackTrace();
                }

                @Override
                public void onResponse(@NonNull Call call, @NonNull Response response) {
                    runOnUiThread(() -> {
                        try {
                            Tools.toast(new JSONObject(response.body().string()).getString("datalist"));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        refresh(true, null, null);
                    });
                }
            });
        }, (v, position) -> {
            List<HttpParaBean> para=new ArrayList<>();
            para.add(new HttpParaBean("id",String.valueOf(data.get(position).manageTipOffPostBean.id)));
            para.add(new HttpParaBean("token","token"));
            Network.post(para, "admin/getPostById", new Callback() {
                @Override
                public void onFailure(@NonNull Call call, @NonNull IOException e) {
                    e.printStackTrace();
                }
                @Override
                public void onResponse(@NonNull Call call, @NonNull Response response) {
                    runOnUiThread(() -> {
                        try {
                            JsonObject jo = JsonParser.parseString(response.body().string()).getAsJsonObject();
                            if (jo.get("code").getAsInt()==2726297){
                                Tools.toast(jo.get("datalist").getAsString());
                            }else {
                                JsonObject datalist = jo.get("datalist").getAsJsonObject();
                                JsonArray jo_imgs = datalist.get("imgs").getAsJsonArray();
                                String img_url="";
                                if (jo_imgs.size()>0) img_url= jo_imgs.get(0).getAsString();
                                String bannedCount = "0";
                                if (datalist.has("bannedCount")) bannedCount=datalist.get("bannedCount").getAsString();
                                MyApplication.postBean=new HomeListBean(0,datalist.get("bp_content").getAsString(), img_url, datalist.get("bp_report").getAsString(), datalist.get("bp_sort").getAsString(), datalist.get("bp_title").getAsString(), datalist.get("bp_type").getAsString(), datalist.get("bu_id").getAsInt(), datalist.get("id").getAsString(), datalist.get("bp_click_number").getAsInt(), datalist.get("time").getAsString(), datalist.get("wb_nickname").getAsString(), datalist.get("wb_url").getAsString(),bannedCount,null);
                                startActivity(new Intent(ManageTipOffActivity.this, PostDetailActivity.class));
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    });
                }
            });
        });
        binding.manageTipoffList.setOnGroupCollapseListener(i -> data.get(i).manageTipOffPostBean.isColse=true);
        binding.manageTipoffList.setOnGroupExpandListener(i -> data.get(i).manageTipOffPostBean.isColse=false);
        binding.manageTipoffList.setAdapter(adp);
        refresh(true,null,null);
    }
    private void refresh(boolean isrefresh,RefreshLayout loadmore,RefreshLayout refresh) {
        if (isrefresh){
            data.clear();
            current=1;
        }else {
            current++;
        }
        List<HttpParaBean> para=new ArrayList<>();
        para.add(new HttpParaBean("token","token"));
        para.add(new HttpParaBean("size","5"));
        para.add(new HttpParaBean("current",String.valueOf(current)));
        Network.post(para, "admin/getTipOffPost", new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                e.printStackTrace();
            }
            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) {
                runOnUiThread(() -> {
                    try {
                        JSONArray ja = new JSONObject(response.body().string()).getJSONArray("datalist");
                        for (int i = 0; i < ja.length(); i++) {
                            JSONObject jo = ja.getJSONObject(i);
                            List<ManageTipOffReasonBean> reasons=new ArrayList<>();
                            JSONArray ja1 = jo.getJSONArray("tipOffContent");
                            for (int i1 = 0; i1 < ja1.length(); i1++) {
                                JSONObject jo1 = ja1.getJSONObject(i1);
                                reasons.add(new ManageTipOffReasonBean(jo1.getString("wr_reason"),jo1.getString("time"),jo1.getString("wr_type")));
                            }
                            data.add(new ManageTipOffBean(new ManageTipOffPostBean(true,jo.getInt("id"),jo.getString("content"),jo.getString("wb_nickname"),jo.getInt("tipOffCount"), jo.getString("wb_url"),jo.getString("title"),true), reasons));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    adp.notifyDataSetChanged();
                    if (loadmore!=null)loadmore.finishLoadMore();
                    if (refresh!=null)refresh.finishRefresh();
                });
            }
        });
    }
}